package com.cqie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqie.dto.entrepot.EntrepotRecordInfoListDto;
import com.cqie.dto.entrepot.AddEntrepotRecordDto;
import com.cqie.entity.EntrepotRecordInfo;
import com.cqie.exception.SpecialException;
import com.cqie.mapper.EntrepotRecordInfoMapper;
import com.cqie.service.IEntrepotRecordInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.service.IMedicalDrugInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 仓库进取记录表 服务实现类
 * </p>
 *
 * @author 不吃海鲜
 * @since 2024-12-19
 */
@Service
public class EntrepotRecordInfoServiceImpl extends ServiceImpl<EntrepotRecordInfoMapper, EntrepotRecordInfo> implements IEntrepotRecordInfoService {

    @Resource
    private EntrepotRecordInfoMapper entrepotRecordInfoMapper;

    private final IMedicalDrugInfoService medicalDrugInfoService;

    public EntrepotRecordInfoServiceImpl(IMedicalDrugInfoService medicalDrugInfoService) {
        this.medicalDrugInfoService = medicalDrugInfoService;
    }

    @Override
    public List<EntrepotRecordInfoListDto> getEntrepotRecordInfoByWarehouseId(String warehouseId) {
        return null;
    }

    /**
     * 新增进货记录
     *
     * @param addEntrepotRecordDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveStockRecord(AddEntrepotRecordDto addEntrepotRecordDto) throws SpecialException {

        // 仓库id不能为空
        if (ObjectUtils.isEmpty(addEntrepotRecordDto.getStockWarehouseId())) {
            throw new SpecialException("仓库id不能为空");
        }
        // 药品id不能为空
        if (ObjectUtils.isEmpty(addEntrepotRecordDto.getDrugId())) {
            throw new SpecialException("药品id不能为空");
        }
        // 操作数量不能为0或负数
        if (addEntrepotRecordDto.getOperationNumber() <= 0) {
            throw new SpecialException("操作数量不能为0或负数");
        }

        return entrepotRecordInfoMapper.saveStockRecord(addEntrepotRecordDto);

    }

    /**
     * 根据仓库id查询进货取货记录
     *
     * @param warehouseId
     * @return
     */
    @Override
    public List<EntrepotRecordInfo> getEntrepotRecordInfoByMedicineId(String warehouseId, String drugCode,
                                                                             Integer pageSize, Integer pageIndex)
            throws SpecialException {

        // 获取drugIds
        List<Integer> drugIds = medicalDrugInfoService.getMedicalDrugInfoByDrugCode(drugCode)
                .stream()
                .map(s -> s.getDrugId())
                .distinct()
                .toList();

        if (ObjectUtils.isEmpty(drugIds)) {
            throw new SpecialException("没有该药品");
        }

//        // 记录结果List
//        var recordResult = entrepotRecordInfoMapper.getEntrepotRecordInfoByMedicineId(warehouseId, drugIds);
//
//        if (ObjectUtils.isEmpty(recordResult)) {
//            return new ArrayList<>();
//        }

        Page<EntrepotRecordInfo> page = new Page<>(pageIndex, pageSize);

        // 创建 LambdaQueryWrapper 实例
        LambdaQueryWrapper<EntrepotRecordInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntrepotRecordInfo::getRmSign, "0")
                .eq(EntrepotRecordInfo::getWarehouseId, warehouseId)
                        .in(!ObjectUtils.isEmpty(drugIds),EntrepotRecordInfo::getDrugId, drugIds);

        IPage<EntrepotRecordInfo> recordResult = entrepotRecordInfoMapper.selectPage(page, queryWrapper);


        return recordResult.getRecords();

    }
}
